/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package message;

import java.text.ParseException;
import java.util.Date;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class Message implements MessageInterface
{
    private String name;
    private String subject;
    private String sender;
    private String receiver;
    private Date sendDate;
    private boolean read;
    /**
     * Constructor is used to initialized new empty message
     */
    public Message()
    {
        Calendar now = Calendar.getInstance();
        name = now.getTimeInMillis()+"";
        subject = "";
        sender = "";
        receiver = "";
        sendDate = new Date();
        read = false;           //message doesn't read
    }
    /**
     * This method is used to get message name
     * @return
     */
    public String getName()
    {
        return name;
    }
    /**
     * This method is used to set message name
     * @param name
     */
    public void setName(String name)
    {
        this.name = name;
    }
    /**
     * This method is used to know if this message is read before or not
     * @return true if this message read before else return false
     */
    public boolean isRead()
    {
        return read;
    }
    /**
     * This method is used to set message read
     * @param read set if this message read before of not
     */
    public void setRead(boolean read)
    {
        this.read = read;
    }
    /**
     * This method is used to get message receiver
     * @return receiver mail
     */
    public String getReceiver()
    {
        return receiver;
    }
    /**
     * This method is used to set message receiver
     * @param receiver new message receiver
     */
    public void setReceiver(String receiver)
    {
        this.receiver = receiver;
    }
    /**
     * This method is used to get message sent date
     * @return message send date
     */
    public Date getSendDate()
    {
        return sendDate;
    }
    /**
     * This method is used to set message send date
     * @param sendDate new message send date
     */
    public void setSendDate(Date sendDate)
    {
        this.sendDate = sendDate;
    }
    /**
     * This method is used to get message sender
     * @return sender mail
     */
    public String getSender()
    {
        return sender;
    }
    /**
     * This method is used to set message sender
     * @param sender
     */
    public void setSender(String sender)
    {
        this.sender = sender;
    }
    /**
     * This method is used to get message subject
     * @return message subject
     */
    public String getSubject()
    {
        return subject;
    }
    /**
     * This method is used to set message subject
     * @param subject new message subject
     */
    public void setSubject(String subject)
    {
        this.subject = subject;
    }
    /**
     * This method is used to read message information from file
     * and set all message data from data which read from file
     * @param m file which contains message data(messInfo file which exist in every message)
     * @throws java.io.IOException
     */
    public void readMessageInfo(File m)throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader(m));
        SimpleDateFormat f = new SimpleDateFormat("yyyy-mm-dd");
        String[]s = (in.readLine()).split(",");
        this.setName(s[0]);
        this.setSubject(s[1]);
        this.setReceiver(s[2]);
        this.setSender(s[3]);
        try
        {
            this.setSendDate(f.parse(s[4]));
        }
        catch (ParseException ex)
        {
            Logger.getLogger(Message.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setRead(Boolean.parseBoolean(s[5]));
        in.close();
    }
    /**
     *          Excess Code
     * This method is used to set some of  message data
     * @param s array that contains message subject and receiver
     * @param messageSender  message Sender
     */
    public void setMessageData(String[]s ,String messageSender)
    {
        this.setName("");               //??????????????????????????
        this.setSubject(s[0]);
        this.setReceiver(s[1]);
        this.setSender(messageSender);
    }

}
